<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="酒款" prop="drinkName">
        <el-input
          v-model="queryParams.drinkName"
          placeholder="请输入酒款"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类别" prop="category">
        <el-select v-model="queryParams.category" placeholder="请选择类别" clearable>
          <el-option
            v-for="dict in dict.type.drink_category"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="成本" prop="costPrice">
        <el-input
          v-model="queryParams.costPrice"
          placeholder="请输入成本"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="售价" prop="sellPrice">
        <el-input
          v-model="queryParams.sellPrice"
          placeholder="请输入售价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="特色酒款" prop="isFeatured">
        <el-select v-model="queryParams.isFeatured" placeholder="请选择特色酒款" clearable>
          <el-option
            v-for="dict in dict.type.special_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="drinkStatus">
        <el-select v-model="queryParams.drinkStatus" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.undercarriage_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="daterangeCreatedDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="更新时间">
        <el-date-picker
          v-model="daterangeUpdatedDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['drink:drink:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['drink:drink:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['drink:drink:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['drink:drink:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="drinkList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="酒款编号" align="center" prop="drinkId" />
      <el-table-column label="酒款" align="center" prop="drinkName" />
      <el-table-column label="图片" align="center" prop="picture" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.picture" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="描述" align="center" prop="description" />
      <el-table-column label="类别" align="center" prop="category">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.drink_category" :value="scope.row.category"/>
        </template>
      </el-table-column>
      <el-table-column label="成本" align="center" prop="costPrice" />
      <el-table-column label="售价" align="center" prop="sellPrice" />
      <el-table-column label="特色酒款" align="center" prop="isFeatured">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.special_yes_no" :value="scope.row.isFeatured"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="drinkStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.undercarriage_yes_no" :value="scope.row.drinkStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="部门ID" align="center" prop="deptId" />
      <el-table-column label="创建时间" align="center" prop="createdDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updatedDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updatedDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['drink:drink:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['drink:drink:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改酒品管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="酒款" prop="drinkName">
          <el-input v-model="form.drinkName" placeholder="请输入酒款" />
        </el-form-item>
        <el-form-item label="图片" prop="picture">
          <image-upload v-model="form.picture"/>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="类别" prop="category">
          <el-select v-model="form.category" placeholder="请选择类别">
            <el-option
              v-for="dict in dict.type.drink_category"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="成本" prop="costPrice">
          <el-input v-model="form.costPrice" placeholder="请输入成本" />
        </el-form-item>
        <el-form-item label="售价" prop="sellPrice">
          <el-input v-model="form.sellPrice" placeholder="请输入售价" />
        </el-form-item>
        <el-form-item label="特色酒款" prop="isFeatured">
          <el-radio-group v-model="form.isFeatured">
            <el-radio
              v-for="dict in dict.type.special_yes_no"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="状态" prop="drinkStatus">
          <el-radio-group v-model="form.drinkStatus">
            <el-radio
              v-for="dict in dict.type.undercarriage_yes_no"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDrink, getDrink, delDrink, addDrink, updateDrink } from "@/api/drink/drink";

export default {
  name: "Drink",
  dicts: ['special_yes_no', 'drink_category', 'undercarriage_yes_no'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 酒品管理表格数据
      drinkList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态时间范围
      daterangeCreatedDate: [],
      // 状态时间范围
      daterangeUpdatedDate: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        drinkName: null,
        category: null,
        costPrice: null,
        sellPrice: null,
        isFeatured: null,
        drinkStatus: null,
        createdDate: null,
        updatedDate: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        drinkName: [
          { required: true, message: "酒款不能为空", trigger: "blur" }
        ],
        category: [
          { required: true, message: "类别不能为空", trigger: "change" }
        ],
        sellPrice: [
          { required: true, message: "售价不能为空", trigger: "blur" }
        ],
        isFeatured: [
          { required: true, message: "特色酒款不能为空", trigger: "change" }
        ],
        drinkStatus: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
        createdDate: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        updatedDate: [
          { required: true, message: "更新时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询酒品管理列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreatedDate && '' != this.daterangeCreatedDate) {
        this.queryParams.params["beginCreatedDate"] = this.daterangeCreatedDate[0];
        this.queryParams.params["endCreatedDate"] = this.daterangeCreatedDate[1];
      }
      if (null != this.daterangeUpdatedDate && '' != this.daterangeUpdatedDate) {
        this.queryParams.params["beginUpdatedDate"] = this.daterangeUpdatedDate[0];
        this.queryParams.params["endUpdatedDate"] = this.daterangeUpdatedDate[1];
      }
      listDrink(this.queryParams).then(response => {
        this.drinkList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        drinkId: null,
        drinkName: null,
        picture: null,
        description: null,
        category: null,
        costPrice: null,
        sellPrice: null,
        isFeatured: null,
        drinkStatus: null,
        deptId: null,
        createdDate: null,
        updatedDate: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreatedDate = [];
      this.daterangeUpdatedDate = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.drinkId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加酒品管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const drinkId = row.drinkId || this.ids
      getDrink(drinkId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改酒品管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.drinkId != null) {
            updateDrink(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDrink(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const drinkIds = row.drinkId || this.ids;
      this.$modal.confirm('是否确认删除酒品管理编号为"' + drinkIds + '"的数据项？').then(function() {
        return delDrink(drinkIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('drink/drink/export', {
        ...this.queryParams
      }, `drink_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
